/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.peersim.pastry.protocol;

/**
 * This message is used to issue requests for fresh leaf-set entries upon discovery
 * of dead leaves. The key of the pastry message is the {@link PastryId} of the
 * dead node.
 *
 * @author frusso
 * @version 0.1
 * @since 0.1
 */
class LeafSetEntryReq extends PastryMessage {

  /**
   * Constructor.
   *
   * @param deadNode The dead node id
   * @param src The source
   * @param dst The destination
   */
  LeafSetEntryReq(PastryId deadNode, PastryNode src, PastryNode dst) {
    super.setType(Type.LEAF_SET_ENTRY_REQ);
    super.setKey(deadNode);
    super.setOrignator(src);
    super.setSender(src);
    super.setDestination(dst);
  }

  /**
   * Answers to the current message with a {@link LeafSetEntryRsp} carrying the
   * sender {@link PastryLeafSet} instance.
   *
   * @param leafSet The sender {@link PastryLeafSet} instance
   * @return The response to the current message
   */
  LeafSetEntryRsp reply(PastryLeafSet leafSet) {
    LeafSetEntryRsp r = new LeafSetEntryRsp(this.getDeadNode(), this.getDst(), this.getOriginalSender(), leafSet);
    r.setOrignator(this.getOriginalSender());
    r.setHops(this.getHops());
    return r;
  }

  /**
   * Returns the {@link PastryId} of the dead node, stored as key in the message.
   * @return The id of the dead node
   */
  PastryId getDeadNode() {
    return super.getKey();
  }

  /**
   * This message is used to reply to requests for fresh leaf-set entries upon discovery
   * of dead leaves. The key of the pastry message is the {@link PastryId} of the
   * dead node.
   *
   * @author frusso
   * @version 0.1
   * @since 0.1
   */
  static class LeafSetEntryRsp extends LeafSetEntryReq {

    private PastryLeafSet leafSet;

    /**
     * Constructor.
     *
     * @param deadNode The id of the dead-node
     * @param src The sender
     * @param dst The destination
     * @param leafSet The sender's leaf-set
     */
    private LeafSetEntryRsp(PastryId deadNode, PastryNode src, PastryNode dst, PastryLeafSet leafSet) {
      super(deadNode, src, dst);
      super.setType(Type.LEAF_SET_ENTRY_RSP);
      this.leafSet = leafSet;
    }

    /**
     * Returns the leaf-set stored in the message.
     * @return The leafSet
     */
    public PastryLeafSet getLeafSet() {
      return leafSet;
    }
  }
}